$(function() {
	var entrust_company_list=[];
	var entrust_company_optiont='';
	$("#loading-first").fadeOut(500);
	$('button').on('click',function(){
	 	if($(this).data('type')=='file'){
	 		$(this).parent().find('input[type=file]').click()
	 	}
	});
	{if $item.area_id}
	console.log({$item.area_id})
	for(var areaidx=0;areaidx<$('.area').find('option').length;areaidx++){
		$('.area').find('option').eq(areaidx).attr('selected', false);
	}
	$('.area').find('option').eq({$item.area_id-1}).attr('selected', true);
	{/if}
	{if $item.car_model_id}
	{else/}
		var brandid=$('.brand select').val();
		
		$.ajax({
			method: "POST",
			url:"{:url('adminapi/car/brands')}",
			data:{
				id:brandid
			},
			success:function(res){
				console.log(res);
					$('.model select').html(res);
			}
		});
	{/if}
	//获取车型
	$.ajax({
		type:'post',
		url:"{:url('adminapi/Car/cartypes')}",
		data:{},
		success:function(res){
			console.log(res)
			var list=`<option '>请选择车型</option>`;
			for(var key in res.data){
				{if $Think.get.car_id}
				if(key=={$item.car_type_id}){
					list+=`<option selected value='${key}'>${res.data[key]}</option>`
				}else{
					list+=`<option value='${key}'>${res.data[key]}</option>`
				}
				{else/}
				list+=`<option value='${key}'>${res.data[key]}</option>`
				{/if}
				list+=`<option value='${key}'>${res.data[key]}</option>`
			}
			$('.car_type_id').html(list)
		},
		error:function(){
			Err('请求失败')
		}
	})
	//获取委托公司
	$.ajax({
		type:'post',
		url:"{:url('adminapi/Company/companyList')}",
		data:{
			page:1,
			pagenum:999999,
			where_json:JSON.stringify({
				company_mode:['in',"2"]
			})
		},
		success:function(res){
			console.log(res);
			entrust_company_list=res.data
			{if $item.entrust_company_id}
			$('.entrust_company').css('display','block');
			entrust_company_option=`<option>请选择委托公司</option>`
			for(var i=0;i<entrust_company_list.length;i++){
				if(entrust_company_list[i].company_id=="{$item.entrust_company_id}"){
					entrust_company_option+=`<option selected value='${entrust_company_list[i].company_id}'>${entrust_company_list[i].company_name}</option>`
				}else{
					entrust_company_option+=`<option value='${entrust_company_list[i].company_id}'>${entrust_company_list[i].company_name}</option>`
				}
			
				
			}
			$('select[name="entrust_company_id"]').html(entrust_company_option)
			 $('select[name="entrust_company_id"]').selected({
			    btnWidth: '100%',
			    btnSize: 'sm',
			    searchBox: "1",
			   	maxHeight: "240px"
			  });
			{/if}
		},
		error:function(){
			Err('请求失败');
		}
	})
	$('.ziyou').on('change',function(){
		
		if($(this).val()==1){
			$(this).parent().addClass('am-u-sm-12');
			$(this).parent().removeClass('am-u-sm-4');
			$('.entrust_company').css('display','none');
			$('select[name="entrust_company_id"]').val("")
			$('select[name="entrust_company_id"]').find('option').each(function(){
				if($(this).index()==0){
					$(this).attr('selected',true)
				}else{
					$(this).removeAttr('selected')
				}
			})
		}else{
			$(this).parent().addClass('am-u-sm-4');
			$(this).parent().removeClass('am-u-sm-12');
			$('.entrust_company').css('display','block');
			entrust_company_option=`<option>请选择委托公司</option>`
			for(var i=0;i<entrust_company_list.length;i++){
				{if $item.entrust_company_id}
					if(entrust_company_list[i].company_id=="{$item.entrust_company_id}"){
						entrust_company_option+=`<option selected value='${entrust_company_list[i].company_id}'>${entrust_company_list[i].company_name}</option>`
					}else{
					entrust_company_option+=`<option value='${entrust_company_list[i].company_id}'>${entrust_company_list[i].company_name}</option>`
					}
				
					
				{else/}
				entrust_company_option+=`<option value='${entrust_company_list[i].company_id}'>${entrust_company_list[i].company_name}</option>`
				{/if}
			}
			
			 $('select[name="entrust_company_id"]').html(entrust_company_option)
			 $('select[name="entrust_company_id"]').selected({
			    btnWidth: '100%',
			    btnSize: 'sm',
			    searchBox: "1",
			   	maxHeight: "240px"
			  });
			
		}
	})

//提交
	$('.keep').on('click', function() {
		var treeObj = $.fn.zTree.getZTreeObj("ztreeid");
		var nodes = treeObj.getNodes();
		console.log(nodes);
		var text = [];
		for(var i = 0; i < nodes.length; i++) {
		if(nodes[i].checked) {text.push(nodes[i].id)}
			if(nodes[i].children) {
				for(var j = 0; j < nodes[i].children.length; j++) {
					if(nodes[i].children[j].checked) {text.push(nodes[i].children[j].id)}
					if(nodes[i].children[j].children) {
						for(var z = 0; z < nodes[i].children[j].children.length; z++) {
							if(nodes[i].children[j].children[z].checked) {text.push(nodes[i].children[j].children[z].id)}
						}
					} 
				}
			} 
		}
		
		$('.prop').val(text)
		console.log($('.prop').val())
		{if $Think.get.car_id}
		sendForm($(this), 1);
		{else/}
		sendForm($(this), 1,'{:url("index")}',1);
		{/if}
		
		
	});
	$('.back').on('click', function() {
		window.history.back()
	});
	
	$('.company_name').on('click', function() {
		console.log($('.ziyou').val())
		if($('.ziyou').val()==1){
			choosecar("{:url('company/index',['op'=>'search'])}?own=1")
		}else{
			choosecar("{:url('company/index',['op'=>'search'])}?own=9")
		}
	});
	$('.car_use').on('click', function() {
		choosecar("{:url('company/index',['op'=>'search'])}")
	});
//	$('.car_entrust').on('click', function() {
//		console.log($('.ziyou').val())
//		if($('.ziyou').val()==1){
//			choosecar("{:url('company/index',['op'=>'entrust'])}?own=1")
//		}else{
//			choosecar("{:url('company/index',['op'=>'entrust'])}?own=9")
//		}
//		
//	});
	$('.register').on('click',function(){
		Msg('登记成功')
		window.location.href='{:url("audit")}'
	})
	$('.register_back').on('click',function(){
		
		window.location.href='{:url("audit")}'
	})
	
	$('.ziyou').on('change',function(){
		console.log(11)
		$('.company_id').val("")
		$('.company_name').val("")
		if($(this).val()==1){
			$('.company_label').html('所属公司/单位： ')
			$('.company_id').attr('name','company_id')
		}else{
			$('.company_label').html('委托公司/单位： ')
			$('.company_id').attr('name','entrust_company_id')
		}
	})
	//选择品牌
	
	$('.brand select').on('change',function(){
		var brandid=$(this).val();
		var brand = layer.load(1, {
		  shade: [0.1,'#fff'] //0.1透明度的白色背景
		});
		$.ajax({
			method: "POST",
			url:"{:url('adminapi/car/brands')}",
			data:{
				id:brandid
			},
			success:function(res){
				console.log(res);
				layer.close(brand);
				if(res.code==1){
				  	var list='';
					for(var key in res.data){
						list+=`<option value='${key}'>${res.data[key]}</option>`
					}

                    list = list === '' ? '<option value="">暂无可选项</option>' : list;
					$('.model select').html(list);
				}else{
					layer.close(brand);
					Err(res.msg)
				}
			}
		});
	})
	$('.model select').on('change',function(){
		var model = layer.load(1, {
		  shade: [0.1,'#fff'] //0.1透明度的白色背景
		});
		var modelid=$(this).val();
		console.log(modelid)
		if(modelid){
		$.ajax({
			method: "POST",
			url:"{:url('adminapi/car/brands')}",
			data:{
				id:modelid
			},
			success:function(res){
				layer.close(model);
				  if(res.code==1){
				  	var list='';
					for(var key in res.data){
						list+=`<option value='${key}'>${res.data[key]}</option>`
					}

					list = list === '' ? '<option value="">暂无可选项</option>' : list;
					$('.series select').html(list)
					}else{
						Err(res.msg)
					}
				 
			}
		});
		}else{
			layer.close(model);
			$('.series select').html('')
		}
		layer.close(model);
	})
	$('.buy_date').on('change',function(){
		var newdata=new Date()
		var buydata=new Date($(this).val())
		var year=Math.ceil((newdata-buydata)/1000/3600/24/365)
		$('.car_age').val(year)
	})
	{if $props}
	var setting = {
		check: {
			enable: true,
			chkDisabledInherit: true,
			chkStyle: "radio"
		},
		data: {
			simpleData: {
				enable: true
			}
		}
	};
	var zNodes = {$props};
	$.fn.zTree.init($("#ztreeid"), setting, zNodes);
	{/if}
	
})